﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Nauka : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        kopcinja();
        ispolniGrid();
    }
    protected void kopcinja()
    {
        ((ImageButton)Master.FindControl("imbMakedonija")).ImageUrl = "mk-c.gif";
        ((ImageButton)Master.FindControl("imbPocetna")).ImageUrl = "pocetna-c.gif";
        ((ImageButton)Master.FindControl("imbSvet")).ImageUrl = "svet-c.gif";
        ((ImageButton)Master.FindControl("imbSport")).ImageUrl = "sport-c.gif";
        ((ImageButton)Master.FindControl("imbNauka")).ImageUrl = "nauka-g.gif";
        ((ImageButton)Master.FindControl("imbKultura")).ImageUrl = "kultura-c.gif";
        ((ImageButton)Master.FindControl("imbSouBiznis")).ImageUrl = "sou-biz-c.gif";
    }
    protected void ispolniGrid()
    {
        Baza baza = new Baza();

        string sqlString = "SELECT nastan_id,naslov,datum,[user] " +
                           "FROM nastan " +
                           "WHERE ime_kategorija='Наука'";
        DataSet ds = baza.Zemi(sqlString, "Nastan");

        List<string> SlikiUrl = new List<string>();
        List<string> lista = new List<string>();
        for (int i = 0; i < ds.Tables["Nastan"].Rows.Count; i++)
        {
            string nastan_id = ds.Tables["Nastan"].Rows[i][0].ToString();
            sqlString = "SELECT id_slika " +
                           "FROM slika " +
                           "WHERE nastan_id=" + nastan_id;
            DataSet ds1 = baza.Zemi(sqlString, "Nastan");
            lista.Add(ds1.Tables["Nastan"].Rows[0][0].ToString());
        }
        for (int i = 0; i < lista.Count; i++)
            SlikiUrl.Add("Handler.ashx?id=" + lista[i].ToString());

        DataColumn dc = new DataColumn();
        ds.Tables["Nastan"].Columns.Add("slika");
        for (int i = 0; i < SlikiUrl.Count; i++)
        {
            ds.Tables["Nastan"].Rows[i][4] = SlikiUrl[i];
        }

        ViewState["ds"] = ds;
        gvNauka.DataSource = ds;
        gvNauka.DataBind();


    }
    protected string vratiId(LinkButton lnkButton)
    {
        Baza baza = new Baza();
        string sqlString = "SELECT nastan_id " +
                           "FROM nastan " +
                           "WHERE naslov='" + lnkButton.Text + "'";
        DataSet ds = baza.Zemi(sqlString, "ID");
        string id = ds.Tables["ID"].Rows[0][0].ToString();
        return id;
    }
    protected void gvNauka_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvNauka.PageIndex = e.NewPageIndex;
        gvNauka.SelectedIndex = -1;
        DataSet ds = (DataSet)ViewState["ds"];
        gvNauka.DataSource = ds;
        gvNauka.DataBind();
    }
    protected void Rang(string id)
    {
        Baza baza = new Baza();
        string sqlString = "SELECT rang " +
                         "FROM nastan " +
                         "WHERE nastan_id=" + id;
        DataSet ds = baza.Zemi(sqlString, "Rang");
        int rang = Convert.ToInt32(ds.Tables["Rang"].Rows[0][0].ToString());
        rang = rang + 1;
        sqlString = "UPDATE nastan SET " +
                    "rang=" + rang +
                    "WHERE nastan_id=" + id;
        baza.Update(sqlString);
    } 
    protected void gvNauka_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        LinkButton lnkButton = (LinkButton)gvNauka.Rows[e.NewSelectedIndex].Cells[1].Controls[0];
        Rang(vratiId(lnkButton));
        Response.Redirect("Pregled.aspx?id=" + vratiId(lnkButton));
    }
}